package com.controller;

import com.entity.Doctor;
import com.entity.Patient;
import com.service.DoctorService;
import com.service.PatientService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet(name = "EnrollServlet", value = "/enrollServlet")
public class EnrollServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();

        int result = 0;
        String enrollAccount = request.getParameter("enrollAccount");
        String enrollPwd = request.getParameter("enrollPwd");

//        System.out.println(enrollAccount);
//        System.out.println(enrollPwd);

        Patient patient = new Patient(enrollAccount,enrollPwd);
        PatientService patientService = new PatientService();

        result = patientService.patientEnrollLogin(patient);

        if(result == 1)
        {
            session.setAttribute("status", "1");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }
        else
        {
            request.setAttribute("error_code", result);//返回0失败 返回2有重复的用户
            request.getRequestDispatcher("enrollFailure.jsp").forward(request,response);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
